package work.jame.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import work.jame.blog.framework.exception.BusinessException;
import work.jame.blog.framework.exception.db.UpdateDataBaseException;
import work.jame.blog.framework.utils.security.SpringSecurityUtils;
import work.jame.blog.pojo.UserCommentOperation;
import work.jame.blog.mapper.UserCommentOperationMapper;
import work.jame.blog.service.UserCommentOperationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jame
 * @since 2023-05-20
 */
@Service
public class UserCommentOperationServiceImpl extends ServiceImpl<UserCommentOperationMapper, UserCommentOperation> implements UserCommentOperationService {


    @Resource
    private UserCommentOperationMapper userCommentOperationMapper;

    @Override
    public boolean queryUserLikeComment(Long commentId, Long userId) {
        QueryWrapper<UserCommentOperation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("comment_id", commentId)
                .eq("status", true);
        return this.count(wrapper) == 1;
    }

    @Override
    public boolean queryUserNotLikeComment(Long commentId, Long userId) {
        QueryWrapper<UserCommentOperation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("comment_id", commentId)
                .eq("status", false);
        return this.count(wrapper) == 1;
    }

    @Override
    public boolean deletedByUserIdAndCommentId(Long commentId, Long userId) {
        QueryWrapper<UserCommentOperation> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("comment_id", commentId);
        return this.remove(wrapper);
    }

    @Override
    public boolean likeComment(Long commentId) {
        //先查询是否已经喜欢过该评论
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (this.queryUserLikeComment(commentId, userId)) {
            throw new BusinessException("你已经给该评论点过赞啦");
        }
        //如果用户没有点过不喜欢,则新增,否则修改不喜欢为喜欢
        if (this.queryUserNotLikeComment(commentId, userId)) {
            Integer result = userCommentOperationMapper.updateStatusToLike(commentId, userId);
            return !Objects.isNull(result) && result == 1;
        } else {
            UserCommentOperation operation = new UserCommentOperation();
            operation.setCommentId(commentId);
            operation.setUserId(userId);
            operation.setStatus(true);
            return this.save(operation);
        }
    }

    @Override
    public boolean cancelLikeComment(Long commentId) {
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (!this.queryUserLikeComment(commentId, userId)) {
            throw new BusinessException("你还没有给这条评论点过赞哦");
        }
        return this.deletedByUserIdAndCommentId(commentId, userId);
    }

    @Override
    public boolean notLikeComment(Long commentId) {
        //先查询是否已经不喜欢过该评论
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (this.queryUserNotLikeComment(commentId, userId)) {
            throw new BusinessException("你已经给该评论点过赞啦");
        }
        //如果用户没有点过喜欢,则新增,否则修改喜欢为不喜欢
        if (this.queryUserLikeComment(commentId, userId)) {
            Integer result = userCommentOperationMapper.updateStatusToNotLike(commentId, userId);
            return !Objects.isNull(result) && result == 1;
        } else {
            UserCommentOperation operation = new UserCommentOperation();
            operation.setCommentId(commentId);
            operation.setUserId(userId);
            operation.setStatus(false);
            return this.save(operation);
        }
    }

    @Override
    public boolean cancelNotLikeComment(Long commentId) {
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (!this.queryUserNotLikeComment(commentId, userId)) {
            throw new BusinessException("你没有给这条评论点过不喜欢");
        }
        return this.deletedByUserIdAndCommentId(commentId, userId);
    }
}
